package sprite;

import java.awt.Color;
import java.awt.Graphics;

import data.Cell;
import data.Grid;
import data.Level;

public class WalkingSprite extends Sprite {

	private Cell currentPos;
	private Level level;
	private Grid grid;
	private int hp = 1000;
	private int maxHp = 1000;
	private boolean moving = false;
	
	private final static int CELLSIZE = 50;
	
	public void clocktick() {
		
        super.clocktick();
        if (currentPos.getPosition().equals(level.getEnd().getPosition())) {
        	this.setMotion(0,0);
        	System.out.println("Framme");
        	System.out.println(currentPos.getPosition().getX() + "  " + level.getEnd().getPosition().getX());
        }
        else if (!moving) {
        		if (currentPos.getWalkDirection() == 'E') {
        			this.setCurrentPos(grid.getCell(currentPos.getPosition().x+1, currentPos.getPosition().y));
        			this.setMotion(2, 0);
        			moving = true;
        		}
        		else if (currentPos.getWalkDirection() == 'S') {
        			this.setCurrentPos(grid.getCell(currentPos.getPosition().x, currentPos.getPosition().y+1));
        			this.setMotion(0, 2);
        			moving = true;
        		}
        		else if (currentPos.getWalkDirection() == 'W') {
        			this.setCurrentPos(grid.getCell(currentPos.getPosition().x-1, currentPos.getPosition().y));
        			this.setMotion(-2, 0);
        			moving = true;
        		}
        		else if (currentPos.getWalkDirection() == 'N') {
        			this.setCurrentPos(grid.getCell(currentPos.getPosition().x, currentPos.getPosition().y-1));
        			this.setMotion(0, -2);
        			moving = true;
        		}
        }
        else {
        	if (this.getX() == this.getCurrentPos().getPosition().x*CELLSIZE && 
        			this.getY() == this.getCurrentPos().getPosition().y*CELLSIZE) {
        		this.setMotion(0,0);
        		moving = false;
        	}
        }
    }
	public void drawOn(Graphics g)
    {
		super.drawOn(g);
		Color currentColor = g.getColor();
		g.setColor(Color.BLACK);
		g.drawRect(this.getX(), this.getY() - 12, this.getWidth(), 10);
		g.setColor(Color.RED);
		double percent =  (double)this.hp / (double)this.maxHp;
		double part = percent*(double)(this.getWidth() - 1);
		int hpWidth = (int)part;
		g.fillRect(this.getX() + 1, this.getY() - 11, hpWidth, 9);
		g.setColor(currentColor);
    }

	public void setCurrentPos(Cell currentPos) {
		this.currentPos = currentPos;
	}

	public Cell getCurrentPos() {
		return currentPos;
	}
	public void setLevel(Level level) {
		this.level = level;
		this.grid = level.getGrid();
	}

	public void setHp(int hp) {
		this.hp = hp;
	}

	public int getHp() {
		return hp;
	}
	public void decreaseHp(int minus) {
		this.hp -= minus;
	}
	public void setMaxHp(int maxHp) {
		this.maxHp = maxHp;
	}
	public int getMaxHp() {
		return maxHp;
	}
	
}
